// pages/intro/intro.js
const common = require('../../utils/util.js')
const app = getApp()
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopInfo: {},
        shopComemnt: [],
        shopExperience: [],
        shopWorks: [],
        shopLocation: '',
        shopLatitude: 0,
        shopLongitude: 0,
        userLatitude: 0,
        userLongitude: 0,
        shopDistance: '',
        navType: 1,
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getShopInfo()
        this.getShopExperience()
        this.getShopWorks()
        this.getShopComment()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        this.setData({
            isLoading: false
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getShopComment()
    },

    getShopComment: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let shopComemnt = this.data.shopComemnt
        let data = {
            activity: 785,
            type: 2,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            page: this.data.nowPage,
            limit: 10
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goodscomment/comment_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    shopComemnt: shopComemnt,
                    isAll: true
                })
            } else {
                shopComemnt = shopComemnt.concat(res)
                that.setData({
                    shopComemnt: shopComemnt,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    getShopInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1
        }
        common.post('Store/get_store_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                shopInfo: res,
                shopLocation: res.address
            })
            that.initLocation()
        }).catch(function(e) {})
    },

    getShopExperience: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1
        }
        common.post('Store/get_store_experience', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopExperience: res
            })
        }).catch(function(e) {})
    },

    getShopWorks: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 1,
            type: 3,
            order_by: 'add_time',
            limit: 99,
            page: 0
        }
        common.post('Moments/moment_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopWorks: res
            })
        }).catch(function(e) {})
    },

    goNavigation: function() {
        const that = this
        qqmapsdk.geocoder({
            //获取表单传入地址
            address: that.data.shopLocation, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
            success: function(res) { //成功后的回调
                const latitude = res.result.location.lat
                const longitude = res.result.location.lng
                wx.openLocation({
                    latitude,
                    longitude,
                    name: that.data.shopLocation
                })
            }
        })
    },

    initLocation: function() {
        const that = this
        qqmapsdk = new QQMapWX({
            key: 'CSSBZ-LXEWK-INHJO-AOC4H-FZ7OE-TJFRE'
        })
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                let latitude_my = res.latitude
                let longitude_my = res.longitude
                that.setData({
                    userLatitude: latitude_my,
                    userLongitude: longitude_my
                })
                qqmapsdk.geocoder({
                    //获取表单传入地址
                    address: that.data.shopLocation, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
                    success: function(res) { //成功后的回调
                        let latitude = res.result.location.lat
                        let longitude = res.result.location.lng
                        that.setData({
                            shopLatitude: latitude,
                            shopLongitude: longitude
                        })
                        qqmapsdk.calculateDistance({
                            //mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行），不填默认：'walking',可不填
                            //from参数不填默认当前地址
                            //获取表单提交的经纬度并设置from和to参数（示例为string格式）
                            from: {
                                latitude: that.data.userLatitude,
                                longitude: that.data.userLongitude
                            },
                            to: [{
                                latitude: that.data.shopLatitude,
                                longitude: that.data.shopLongitude
                            }], //终点坐标
                            success: function(res) { //成功后的回调
                                let distance = res.result.elements[0].distance
                                if (distance > 1000) {
                                    that.setData({
                                        shopDistance: (distance / 1000).toFixed(2) + 'Km'
                                    })
                                } else {
                                    that.setData({
                                        shopDistance: distance + 'm'
                                    })
                                }
                            }
                        })
                    }
                })
            }
        })
    },

    goPhoneCall: function(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },

    selectNav: function(e) {
        if (e.detail == this.data.navType) {
            return
        }
        this.setData({
            navType: e.detail
        })
    }
})